package com.sx.micowell.module.login

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.gyf.immersionbar.ImmersionBar
import com.example.mvvm.bar.TitleBar
import com.example.mvvm.until.*
import com.lxj.xpopup.XPopup
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.ActLoginBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyActivity
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.entity.AccountBean
import com.sx.micowell.framework.model.LoginModel
import com.sx.micowell.framework.tools.AppDialogUtils
import com.sx.micowell.framework.tools.EditUtils
import com.sx.micowell.wxapi.WxUtils
import io.reactivex.Observable
import kotlinx.android.synthetic.main.frag_device_data.*

/**
 * @author: YY
 * @time: 2021/8/3
 * @describe 登录
 */
@Route(path = RoutePath.LOGIN)
class LoginActivity : MyActivity<ActLoginBinding>(), TitleBarAction {

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var accountBean: AccountBean? = null

    var isFirst: Boolean = false

    private lateinit var viewModel: LoginModel

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.act_login,  BR.viewModel, viewModel)
    }

    override fun initViewModel() {
        super.initViewModel()
        viewModel = getActivityScopeViewModel(LoginModel::class.java)
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
    }



    override fun initData() {
        if (accountBean != null) {
            viewModel.phoneNumber.set(accountBean?.phone)
            viewModel.loginPassword.set(accountBean?.pwd)

        }

        isFirst  = SPUtils.getInstance().getBoolean(SPKey.isFirstLaunchApp, true)

        if (isFirst) {
            viewModel.userAgreement()
        }

    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 密码显示状态
        viewModel.pwdSwitchEvent.observe(this, {
            EditUtils.showHide(it, binding.editPwd)
        })

        viewModel.loginStateEvent.observe(this, {
            if (it.isPhone) {
                RouteManager.getInstance().intentBindPhone(it)
            } else {
                LogUtils.d("intentToMain")
                RouteManager.getInstance().intentToMain()
                finish()
            }

        })

        viewModel.agreementEvent.observe(this, {
            AppDialogUtils.showPrivacyDialog(
                context, object : AppDialogUtils.IOnClickListener {
                    override fun leftListener() {
                        finish()
                    }
                    override fun rightListener() {
                        SPUtils.getInstance().put(SPKey.isFirstLaunchApp, false)
                    }
                }, object : AppDialogUtils.ITextOnClickListener {
                    override fun onPrivacyOneClick() {
                        val bundle = Bundle()
                        bundle.putSerializable(IntentKey.DATA, it)
                        startContainerActivity(
                            RouteManager.getInstance().getUserAgreementFragment(it).javaClass.canonicalName,
                            bundle
                        )
                    }

                    override fun onPrivacyTwoClick() {
                        val bundle = Bundle()
                        bundle.putSerializable(IntentKey.DATA, it)
                        startContainerActivity(
                            RouteManager.getInstance().getUserAgreementFragment(it).javaClass.canonicalName,
                            bundle
                        )

                    }
                })
        })

    }

    override fun getTitleBar(): TitleBar {
        return binding.title
    }

    override fun onRightClick(v: View?) {
        RouteManager.getInstance().intentRegisterToForgetPwd()
    }
}